import Vue from "vue";
import VueRouter, { RouteConfig } from "vue-router";
import Login from "@/views/login/index.vue";
import Home from "@/views/home/index.vue";
import Main from "@/views/main/index.vue";
import cookies from "@/utils/cookies";
import Content from "@/views/main/Content/index.vue";
Vue.use(VueRouter);

const routes: Array<RouteConfig> = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    component: Login,
  },
  {
    path: "/main",
    component: Main,
    redirect: "/main/content",
    children: [
      {
        path: "content",
        component: Content,
      },
      {
        path: "content",
        component: Content,
      },
    ],
  },
  {
    path: "/home",
    component: Home,
  },
];

const router = new VueRouter({
  // mode:'hash',//history
  routes,
});
//路由白名单
const whiteList = ["/login", "/404"];
//路由前置守卫
router.beforeEach((to, from, next) => {
  const flag = whiteList.includes(to.path);
  // const flag = whiteList.indexOf(to.path) !== -1;
  if (flag) {
    next();
  } else {
    const token: any = cookies.getToken();
    if (token) {
      next();
    } else {
      next("/login");
    }
  }
});
// router.afterEach((from, to) => {});
export default router;
